2021年的UWP(6) 您所在的位置:网站首页 hp hotkey uwp service是什么 2021年的UWP(6)

2021年的UWP(6)

2023-08-12 16:29| 来源: 网络整理| 查看: 265

我们先从FrontUWP工程讲起,AppServiceHandler.cs是我创建的帮助Class,用来处理AppServiceConnectoin的Connected和RequestReceived事件。

复制代码 public void OnBackgroundActivated(AppServiceTriggerDetails details) { Connected?.Invoke(this, new AppServiceConnectionConnectedEventArgs(details.AppServiceConnection)); Connection = details.AppServiceConnection; Connection.RequestReceived += Connection_RequestReceived; }

private void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args) { RequestReceived?.Invoke(this, args); }

复制代码 而OnBackgroundActivated事件则是在App.xaml.cs中,通过override UWP Application对象的OnBackgroundActivated方法来触发。这里是AppServiceConnection连接的起点,即源头。

复制代码 protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) { base.OnBackgroundActivated(args); if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails details) { if (details.CallerPackageFamilyName == Package.Current.Id.FamilyName) { var deferral = args.TaskInstance.GetDeferral(); args.TaskInstance.Canceled += (sender, e) => { deferral?.Complete(); }; AppServiceHandler.Instance.OnBackgroundActivated(details); } } } 复制代码 以上这些在前面几篇中都有提及,这里不再赘述。在UWP工程的MainPage中,我们记录了UWP进程的process id,Desktop Extension段会读取该值,用以检测UWP process的Exit事件,在UWP被关闭时释放资源。同时通过RequestReceived事件来将Desktop Extension反向通知的HotKey的值,通过HotKeyList绑定显示到UWP的界面上。

复制代码 protected async override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); Process process = Process.GetCurrentProcess(); ApplicationData.Current.LocalSettings.Values[“processId”] = process.Id; if (ApiInformation.IsApiContractPresent(“Windows.ApplicationModel.FullTrustAppContract”, 1, 0)) { await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync(); } AppServiceHandler.Instance.RequestReceived += Instance_RequestReceived; }

private async void Instance_RequestReceived(object sender, Windows.ApplicationModel.AppService.AppServiceRequestReceivedEventArgs e) { var message = e.Request.Message; if (message.TryGetValue("HotKey", out object keyCode)) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => { HotKeyList.Add(keyCode.ToString()); }); } }

复制代码 最后不要忘记给FrontUWP工程添加对Windows Desktop Extension for the UWP的引用。

我们转到Desktop这一边,ReverseNotificatio.Desktop是一个WinForms的程序,通过RegisterHotKey这个Win32的API来监听热键。如何实现监听热键我不做过多介绍,具体请参考示例代码。

[DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

同时为了使用AppServiceConnection,添加了对Win10 API的引用,主要是WindowsRuntime和Windows.winmd这两个文件。前者通过Nuget添加,后者请参考《迁移桌面程序到MS Store(4)——桌面程序调用Win10 API》。

我想强调的是,不要在长生命周期的Desktop Extension进程中,去维护一个全局的AppServiceConnection,某软的文档并没有提到细节,但也明确指出AppServiceConnection在UWP进入suspended状态时,可能被释放。我们要做的事情,是在每一次的热键响应事件中,创建新的AppServiceConnection去发送消息。

复制代码 private async void hotkeys_HotkeyPressed(int ID) { var key = Enum.GetName(typeof(VirtualKey), ID);

var message = new ValueSet { { "HotKey", key } }; var connection = new AppServiceConnection { PackageFamilyName = Package.Current.Id.FamilyName, AppServiceName = "ReverseNotificationAppService" }; connection.ServiceClosed += Connection_ServiceClosed; var status = await connection.OpenAsync(); if (status == AppServiceConnectionStatus.Success) { var response = await connection.SendMessageAsync(message); } }

复制代码 不能保存已创建的AppServiceConnection来重复使用,有时会造成不便。但这也正是我将Desktop Extension分为4个场景的原因,针对不同的用途来创建特定类型的background process。

ReverseNotification.Package作为打包工程,我们需要注意添加对FrontUWP和Desktop的引用。以及编辑Package.appxmanifest文件,提供对AppService和Desktop Extension的支持。

复制代码 USB Microphone https://www.soft-voice.com/ Wooden Speakers https://www.zeshuiplatform.com/ 亚马逊测评 www.yisuping.cn 深圳网站建设www.sz886.com



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有